package com.lin.learning.questionmanagement.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.learning.questionmanagement.dto.AddQuestionDto;
import com.lin.learning.questionmanagement.dto.GetQuestionsDto;
import com.lin.learning.questionmanagement.dto.UpdateQuestionDto;
import com.lin.learning.questionmanagement.mapper.QuestionMapper;
import com.lin.learning.questionmanagement.service.QuestionService;
import com.lin.learning.questionmanagement.utils.BeanCopyUtils;
import com.lin.learning.questionmanagement.vo.PageVo;
import com.our.learning.infrastructure.po.Question;
import com.our.learning.infrastructure.pojo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper,Question> implements QuestionService  {


    @Autowired
    private  QuestionMapper questionMapper;


    @Override
    public Question getQuestionByIdLocal(Integer id) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(id),Question::getId,id);
        Question question=getOne(lambdaQueryWrapper);
        return question;
    }

    @Override
    public List getQuestionByIdsLocal(List id) {
        List<Question> questions = questionMapper.selectBatchIds(id);
        return questions;
    }

    @Override
    public List<Question> getQuestionByTypeAndDifficultyLocal(String type) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getType, type);
        return list(lambdaQueryWrapper);
    }


    @Override
    public ResponseResult getQuestionsByType(Integer pageNum, Integer pageSize,String type) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getType,type);
        Page<Question> page=new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);
        List<Question> list=page.getRecords();
        PageVo pageVo=new PageVo(list,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getQuestions(GetQuestionsDto getQuestionsDto) {
        List<Integer> idlist=getQuestionsDto.getIds();
        List<Question> questions = questionMapper.selectBatchIds(idlist);
        // TODO: 2023-06-21 可能还需要加上其他字段,性质还没适配
/*        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getType,type);
        List<Question> questions1 = questionMapper.selectList(lambdaQueryWrapper);*/
        return ResponseResult.okResult(questions);
    }

    @Override
    public ResponseResult questionList(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Question::getId);
        Page<Question> page=new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);
        List<Question> list=page.getRecords();
        PageVo pageVo=new PageVo(list,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addQuestion(AddQuestionDto addQuestionDto) {
        Question question= BeanCopyUtils.copyBean(addQuestionDto,Question.class);
        save(question);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteQuestion(Integer id) {
        if (getById(id)==null){
            // TODO: 2023-06-20 提示没有这题
        }
        removeById(id);
       // questionNatureMapper.deleteById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateQuestion(UpdateQuestionDto updateQuestionDto) {
        Question question=BeanCopyUtils.copyBean(updateQuestionDto,Question.class);
        updateById(question);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getQuestionById(Integer id) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(id),Question::getId,id);
        Question question=getOne(lambdaQueryWrapper);
        return ResponseResult.okResult(question);
    }
}
